﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Rob Ashton 2009
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@mvcex.com
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcEx;
using site.core.Controllers;
using MvcEx.Controllers;
using MvcEx.StructureMap;
using site.lib;

namespace site
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );       
        }


        private IMvcExApplication mApplication;

        public class TestConfigProvider : IMvcExApplicationConfigurationSelector
        {
            #region IMvcExApplicationConfigurationSelector Members

            public string GetActiveConfigurationId()
            {
                return "customertwo";
            }

            #endregion
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);

            // Initialize the MvcExApplication in the most default way
            mApplication = MvcExApplication.Create(HttpRuntime.AppDomainAppPath);

            // Set up structure map
            StructureMap.ObjectFactory.Initialize(
                x =>
                    x.Scan(d =>
                    {
                        d.AssemblyContainingType<ITestService>();
                        d.WithDefaultConventions();
                    }
                   )
            );

            // Set the Ioc Container
            mApplication.ControllerFactory.SetIocContainer(new MvcExStructureMapContainer());

            // Set the configuration provider
            mApplication.SetConfigurationSelector(new TestConfigProvider());

            ModelBinders.Binders[typeof(Home.IndexViewModel)] = new DefaultModelBinder();
        }
    }
}